<template>
  <div v-show="editShow">
    <div class="container">
      <el-row :gutter="20">
        <div class="clearfix">
          <div class="crumbs">
            <i class="el-icon-back"></i>
            <el-button type="text" @click="goBack">返回</el-button>
          </div>
          <div class="btn-save">
            <el-button type="primary" @click="saveClick('ruleForm')">保存</el-button>
          </div>
        </div>
        <div class="form-box">
          <el-form  :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="eidt-el-form">
            <el-form-item label="用户"  prop="accountId">
              <el-select v-model="ruleForm.accountId" placeholder="用户" :filterable="true"  >
                <el-option
                  v-for="(item,index) in users"
                  :label="item.name"
                  :key="index"
                  :value="item.accountId"
                >
                  <div @click="getChange(item)">{{item.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目类型" prop="projectTypeId">
              <el-select v-model="ruleForm.projectTypeId" placeholder="请选择项目类型">
                <el-option
                  v-for="(type,index) in productType"
                  :label="type.name"
                  :key="index"
                  :value="type.projectTypeId">
                  <div @click="getChangeType(type)">{{type.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目" prop="projectId">
              <el-select v-model="ruleForm.projectId" placeholder="请选择项目">
                <el-option
                  v-for="(item,index) in product"
                  :label="item.name"
                  :key="index"
                  :value="item.projectId">
                  <div @click="getProduct(item)">{{item.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="角色" prop="projectRoleId">
              <el-select v-model="ruleForm.projectRoleId" placeholder="请选择角色">
                <el-option
                  v-for="(item,index) in roles"
                  :label="item.name"
                  :value="item.projectRoleId">
                  <div @click="getRoles(item)">{{item.name}}</div>
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </el-row>
    </div>
  </div>
</template>

<script>

export default {
  name: "editprojectmanage",
  data() {
    return {
      rules: {
        projectTypeId: [{ required: true, message: "请选择项目类型", trigger: "blur" }],
        projectId: [{ required: true, message: "请选择项目", trigger: "blur" }],
        projectRoleId: [{ required: true, message: "请选择角色", trigger: "blur" }],
        accountId: [{ required: true, message: "请选择用户", trigger: "blur" }],
      },
      ruleForm:{
        name: "",
        role:"",
        project: "",
        projectTypeId:"",
        projectRoleId:"",
        projectId:"",
        accountId: "",
        accountProjectRoleId:0,
      },
      dataList:[],
      productType: [],
      roles:[],
      users:[],
      product:[]
    };
  },
  props: {
    editShow: {
      type: Boolean,
      default: false
    },
    arrarList: {
      type: Array,
      default() {
        return [];
      }
    },
    editData: {
      type: Object
    }
  },
  watch: {
    editData: function() {
      const item = this.editData;
      this.ruleForm=item;
      if(this.ruleForm.accountProjectRoleId!=0){
        this.$axios.get(`/Project/`)
          .then(res =>{
            if (res.data.hasError == false) {
              this.product = [...res.data.data];
              this.product.forEach((e)=>{
                if(e.projectId==this.ruleForm.projectId){
                  this.ruleForm.projectTypeId=e.projectTypeId;
                }                
              });
            }
          })
        }
        if(this.ruleForm.projectId!=""){
          this.$axios.get(`/ProjectRole/Project/${this.ruleForm.projectId}`)
          .then(res =>{
            if (res.data.hasError == false) {
              this.roles = [...res.data.data];
            }
          })
        }
    }
  },
  created(){
    this.getProductType();
    this.getUsers();
  },
  methods: {
    saveClick(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          if (this.accountProjectRoleId == 0 || this.accountProjectRoleId == undefined ) {
            this.$axios.post("/AccountProjectRole/", this.ruleForm).then(res => {
              if (res.data.hasError == false) {
                this.$message.success("保存成功！");
                this.goBack();
              } else {
                this.$message.error("保存失败！");
              }
            });
          } else {
            
            this.$axios.put("/AccountProjectRole/" + this.accountProjectRoleId , this.ruleForm)
              .then(res => {
                if (res.data.hasError == false) {
                  this.$message.success("编辑成功！");
                  this.goBack();
                } else {
                  this.$message.error("编辑失败！");
                }
              });
          }
        }})
    },
    goBack() {
      this.$emit("backFunStu");
      this.$refs['ruleForm'].resetFields();
      this.ruleForm.accountProjectRoleId=0;
    },
    getProductType() {
      this.$axios.get("/ProjectType/").then(res => {
        if (res.data.hasError == false) {
          this.productType = [...res.data.data];
        }
      });
          
    },
    getUsers(){
       this.$axios.get("/Account/").then(res => {
        if (res.data.hasError == false) {
          this.users = [...res.data.data];
        }
      });
    },
    getChangeType(type){
      this.$axios.get(`/Project/ProjectType/${type.projectTypeId}`)
          .then(res =>{
            if (res.data.hasError == false) {
              this.product = [...res.data.data];
              this.ruleForm.projectId="";
            }
          })
    },
    getRoles(item){
      this.ruleForm.role=item.name;
    },
    getProduct(item){
      this.ruleForm.project=item.name;
      this.projectId=item.projectId;
      this.$axios.get(`/ProjectRole/Project/${item.projectId}`)
          .then(res =>{
            if (res.data.hasError == false) {
              this.roles = [...res.data.data];
              this.ruleForm.projectRoleId="";
            }
          })
    },
    getChange(item){
      this.ruleForm.name=item.name;
      this.ruleForm.accountId=item.accountId;
    },
  }
};
</script>